package com.jy.rhin.support.interceptor;

import com.jy.rhin.sdk.dto.audit.AuditWrapperDto;
import com.jy.rhin.support.interceptor.convertor.AuditInfoConvert;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.ext.logging.event.EventType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 打包审计记录信息参数的格式转换器工程，根据原参数类型，找到对应的转换器。
 * 转换器需要实现 {@link AuditInfoConvert }接口
 * @author young
 */
@Slf4j
@Component
public class AuditInfoConvertFactory  {

   /* *//**
     * 入参格式转换器
     *//*
    private final Map<Type, AuditInfoConvert> converterReqMap = new HashMap<>();
    *//**
     * 出参格式转换器
     *//*
    private final Map<Type, AuditInfoConvert> converterRespMap = new HashMap<>();
*/
	
	@Autowired
	private List<AuditInfoConvert> convers;
    /**
     * 转换，根据入参类型，找到对应的转换器，打包审计记录
     *
     * @param data
     * @return
     */
    public AuditWrapperDto convert(SaveAuditInfoEvent event) {
        /*if (event.getData() == null) {
            throw new NullPointerException("审计记录转换入参不能为空");
        }*/
        for(AuditInfoConvert conver:convers){
        	if(conver.isMatch(event.getMethod(), event.getRequestData())){
        		if (event.getType() == EventType.REQ_IN ){
                	return conver.convertReq(event.getData());
            	}else{
            		return conver.convertResp(event.getData());
            	}
        	}
        }
        /*Class<?> clazz = data.getClass();
        if (converterReqMap.containsKey(clazz)) {
            return converterReqMap.get(clazz).convertReq(data);
        }
        if (converterRespMap.containsKey(clazz)) {
            return converterRespMap.get(clazz).convertResp(data);
        }*/
        //根据出入参选择转换器无法满足所有需求,需要通过方法名和入参确定转化器
        throw new NullPointerException("审计记录转换失败，找不到对应类型的转换器:方法名 = " + event.getMethod());
    }

    
  /*  @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, AuditInfoConvert> convertMap = applicationContext.getBeansOfType(AuditInfoConvert.class);
        for (AuditInfoConvert next : convertMap.values()) {
            Type superclass = next.getClass().getGenericSuperclass();
            ParameterizedType pt = (ParameterizedType) superclass;
            converterReqMap.put(pt.getActualTypeArguments()[0], next);
            converterRespMap.put(pt.getActualTypeArguments()[1], next);
        }
    }*/
}
